package com.park.controller;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.model.Paging;
import com.park.model.ParkFeeCriterion;
import com.park.service.ParkFeeCriterionService;
import com.park.service.Utility;

@Controller
@RequestMapping("/parkFeeCriterion")
public class ParkFeeCriterionController {
	@Autowired
	private ParkFeeCriterionService parkFeeCriterionService;
	private static Log logger = LogFactory.getLog(ParkFeeCriterionController.class);

	@RequestMapping(value = "/insert", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String insert(@RequestBody ParkFeeCriterion criterion) {
		logger.info("insert入口参数:" + criterion);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int num = parkFeeCriterionService.insert(criterion);
		if (num > 0) {
			resultMap.put("status", 1001);
			resultMap.put("message", "添加成功");
		} else {
			resultMap.put("status", 1002);
			resultMap.put("message", "添加失败");
		}
		logger.info("insert出口返回值:" + resultMap);
		return Utility.gson.toJson(resultMap);
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String update(@RequestBody ParkFeeCriterion criterion) {
		logger.info("update入口参数:" + criterion);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int num = parkFeeCriterionService.updateByPrimaryKey(criterion);
		if (num > 0) {
			resultMap.put("status", 1001);
			resultMap.put("message", "修改成功");
		} else {
			resultMap.put("status", 1002);
			resultMap.put("message", "修改失败");
		}
		logger.info("update出口返回值:" + resultMap);
		return Utility.gson.toJson(resultMap);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String delete(@RequestBody Map<String, Object> args) {
		logger.info("delect入口参数:" + args);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int num = parkFeeCriterionService.deleteByPrimaryKey((int) args.get("id"));
		if (num > 0) {
			resultMap.put("status", 1001);
			resultMap.put("message", "删除成功");
		} else {
			resultMap.put("status", 1002);
			resultMap.put("message", "删除失败");
		}
		logger.info("delete出口返回值:" + resultMap);
		return Utility.gson.toJson(resultMap);
	}

	@RequestMapping(value = "/selectPage", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String selectPage(@RequestBody Map<String, Object> args) {
		logger.info("selectPage入口参数:" + args);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			int startPage = (int) args.get("startPage");
			int pageSize = (int) args.get("pageSize");
			String name = (String) args.get("name");
			int type = (int) args.get("type");
			int id = (int) args.get("id");
			List<ParkFeeCriterion> parkFeeCriterions = parkFeeCriterionService.selectPage((startPage - 1) * pageSize,
					pageSize, name, id ,type);
			int allCount = parkFeeCriterionService.getAllCount(name, id, type);
			Paging page = new Paging(startPage, pageSize, allCount, parkFeeCriterions);
			resultMap.put("status", 1001);
			resultMap.put("data", page);
		} catch (Exception e) {
			logger.info(e.getMessage());
			resultMap.put("status", 1002);
			resultMap.put("message", e.getMessage());
		}
		logger.info("selectPage出口返回值:" + resultMap);
		return Utility.gson.toJson(resultMap);
	}
}
